package cn.jsj.invite.controller;

import cn.jsj.invite.interceptor.swich.PassToken;
import cn.jsj.invite.service.PictureFileService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletResponse;

@RestController
@Slf4j
@CrossOrigin
public class PictureController {
//    @Value("${photo.save-path}")
//    private String photoSavePath;

    private final PictureFileService pictureFileService;

    public PictureController(PictureFileService pictureFileService) {
        this.pictureFileService = pictureFileService;
    }

    //图片下载，一次下载一张
    @PassToken
    @GetMapping("/pic/download")
    public void pictureDownload(String picName, HttpServletResponse response) {
        pictureFileService.getPictureFile(picName, response);
//        ServletOutputStream outputStream = null;
//        FileInputStream fileInputStream = null;
//        try {
//            File file = new File(photoSavePath + picName);
//            fileInputStream = new FileInputStream(file);
//            response.setHeader("Content-Disposition", "attachment;filename=" + picName);
//            outputStream = response.getOutputStream();
//            IOUtils.copy(fileInputStream, outputStream);
//        } catch (IOException e) {
//            throw new RuntimeException(e);
//        } finally {
//            try {
//                assert outputStream != null;
//                outputStream.flush();
//                outputStream.close();
//                fileInputStream.close();
//            } catch (IOException e) {
//                log.error("下载流资源回收失败",e);
//            }
//        }

    }

}
